//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package libcore.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Build.VERSION;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.regex.Pattern;

public class ImmersiveUtil {
  private static int sStatusBarHeight = -1;

  public static void setStatusBarTranslucent(Window window) {
    if (VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
      return;
    }

    // 5.0以上，避免显示半透明遮罩
    if (VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      window.clearFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
      window.addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      window.setStatusBarColor(Color.TRANSPARENT);

      int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
      systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
      systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
      window.getDecorView().setSystemUiVisibility(systemUiVisibility);
    } else {
      window.addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
  }

  /** 设置状态栏darkMode,字体颜色及icon变黑(目前支持MIUI6以上,Flyme4以上,Android M以上) */
  public static void setStatusBarDarkMode(Window window) {
    if (isFlyme4Later()) {
      setStatusBarDarkModeForFlyme4(window, true);
    } else if (isMIUI6Later()) {
      setStatusBarDarkModeForMIUI6(window, true);
    } else if (VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      setStatusBarDarkModeForM(window);
    }
  }

  /** 判断是否Flyme4以上 */
  public static boolean isFlyme4Later() {
    return Build.FINGERPRINT.contains("Flyme_OS_4") || VERSION.INCREMENTAL.contains(
      "Flyme_OS_4") || Pattern.compile("Flyme OS [4|5]", Pattern.CASE_INSENSITIVE)
      .matcher(Build.DISPLAY)
      .find();
  }

  /** 判断是否为MIUI6以上 */
  public static boolean isMIUI6Later() {
    try {
      Class<?> clz = Class.forName("android.os.SystemProperties");
      Method mtd = clz.getMethod("get", String.class);
      String val = (String) mtd.invoke(null, "ro.miui.ui.version.name");
      val = val.replaceAll("[vV]", "");
      int version = Integer.parseInt(val);
      return version >= 6;
    } catch (Exception e) {
      return false;
    }
  }

  //------------------------->

  /** android 6.0设置字体颜色 */
  @TargetApi(Build.VERSION_CODES.M)
  private static void setStatusBarDarkModeForM(Window window) {
    window.clearFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.TRANSPARENT);

    int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
    systemUiVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
    window.getDecorView().setSystemUiVisibility(systemUiVisibility);
  }

  /**
   * 设置Flyme4+的darkMode,darkMode时候字体颜色及icon变黑
   * http://open-wiki.flyme.cn/index.php?title=Flyme%E7%B3%BB%E7%BB%9FAPI
   */
  private static boolean setStatusBarDarkModeForFlyme4(Window window, boolean dark) {
    boolean result = false;
    if (window != null) {
      try {
        LayoutParams e = window.getAttributes();
        Field darkFlag =
          LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
        Field meizuFlags = LayoutParams.class.getDeclaredField("meizuFlags");
        darkFlag.setAccessible(true);
        meizuFlags.setAccessible(true);
        int bit = darkFlag.getInt(null);
        int value = meizuFlags.getInt(e);
        if (dark) {
          value |= bit;
        } else {
          value &= ~bit;
        }

        meizuFlags.setInt(e, value);
        window.setAttributes(e);
        result = true;
      } catch (Exception var8) {
      }
    }

    return result;
  }

  /**
   * 设置MIUI6+的状态栏是否为darkMode,darkMode时候字体颜色及icon变黑
   * http://dev.xiaomi.com/doc/p=4769/
   */
  private static void setStatusBarDarkModeForMIUI6(Window window, boolean darkmode) {
    Class<? extends Window> clazz = window.getClass();
    try {
      int darkModeFlag = 0;
      Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
      Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
      darkModeFlag = field.getInt(layoutParams);
      Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
      extraFlagField.invoke(window, darkmode ? darkModeFlag : 0, darkModeFlag);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void setNavigationBarTranslucent(Window win) {
    if (VERSION.SDK_INT >= 19) {
      LayoutParams winParams = win.getAttributes();
      winParams.flags |= 134217728;
      win.setAttributes(winParams);
    }
  }

  public static int getStatusBarHeightIfNeeded() {
    if (!isSupported()) {
      return 0;
    } else if (sStatusBarHeight != -1) {
      return sStatusBarHeight;
    } else {
      try {
        sStatusBarHeight = Resources.getSystem()
          .getDimensionPixelSize(
            Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
      } catch (Throwable var1) {
        sStatusBarHeight = dip2px(25.0F);
      }

      if (sStatusBarHeight <= 0) {
        sStatusBarHeight = dip2px(25.0F);
      }

      return sStatusBarHeight;
    }
  }

  /**
   * 底部虚拟导航栏的高度
   */
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  public static int getNavigationBarHeight(Activity activity) {
    if (VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) return 0;
    DisplayMetrics metrics = new DisplayMetrics();
    //这个方法获取可能不是真实屏幕的高度
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int usableHeight = metrics.heightPixels;
    //获取当前屏幕的真实高度
    activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    int realHeight = metrics.heightPixels;
    if (realHeight > usableHeight) {
      return realHeight - usableHeight;
    } else {
      return 0;
    }
  }

  public static int dip2px(float dpValue) {
    float scale = Resources.getSystem().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5F);
  }

  public static boolean isSupported() {
    return VERSION.SDK_INT >= 19;
  }
}
